<template>
  <div id="main-layout">
    <el-container style="height:100vh">
      <!-- 侧边栏 -->
      <el-aside
          :style="{ width: isCollapsed ? '80px' : '180px' }"
          class="sidebar"
          @mouseenter="isCollapsed = false"
          @mouseleave="isCollapsed = true"
      >

        <!-- 真正的菜单 -->
        <el-menu
            :default-active="activeMenu"
            router
            class="menu"
            :collapse="isCollapsed"
            @select="handleMenuSelect"
        >
          <!-- 自定义 Logo 区域 -->
<!--          <div class="logo" @click="$router.push('/upload')">-->
<!--            <i class="el-icon-upload"></i>-->
<!--            <span v-if="!isCollapsed" class="logo-text">上传分析</span>-->
<!--          </div>-->
          <el-menu-item index="/upload">
            <i class="el-icon-upload" style="color: #5842f0;"></i>
            <span slot="title">上传文件</span>
          </el-menu-item>
          <el-menu-item index="/systemPaper">
            <img src="@/assets/icon/manage.png" alt="论文库" class="menu-icon">
            <span slot="title">论文库</span>
          </el-menu-item>
          <el-menu-item index="/myPaper">
            <img src="@/assets/icon/pdf.png" alt="我的论文" class="menu-icon">
            <span slot="title">我的论文</span>
          </el-menu-item>
          <el-menu-item index="/notes">
            <img src="@/assets/icon/deepseek.png" alt="智读笔记" class="menu-icon">
            <span slot="title">智读笔记</span>
          </el-menu-item>
          <el-menu-item index="/help">
            <i class="el-icon-question" style="color: #5842f0;"></i>
            <span slot="title">帮助</span>
          </el-menu-item>
        </el-menu>

        <!-- 用户信息和登出区域 -->
        <div class="user-section">
          <div class="user-info" v-if="!isCollapsed">
            <div class="username">{{ currentUsername }}</div>
            <div class="user-role">用户</div>
          </div>
          <el-button 
            type="text" 
            class="logout-btn" 
            @click="handleLogout"
            :title="isCollapsed ? '登出' : ''"
          >
            <img src="@/assets/icon/log-out.png" alt="登出" class="logout-icon">
            <span v-if="!isCollapsed">登出</span>
          </el-button>
        </div>
      </el-aside>

      <el-container>
        <!-- 主内容区 -->
        <el-main class="main-content">
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { logout, logoutFromServer, getCurrentUsername } from '@/utils/auth.js';

export default {
  name: 'MainLayout',
  data() {
    return {
      isCollapsed: true,
      activeMenu: this.$route.path,
      avatar: '',
      currentUsername: getCurrentUsername() || '用户'
    };
  },
  watch: {
    '$route.path'(p) {
      this.activeMenu = p;
    }
  },
  methods: {
    handleMenuSelect(path) {
      if (this.$route.path !== path) {
        this.$router.push(path).catch(e => {
          if (e.name !== 'NavigationDuplicated') console.error(e);
        });
      }
    },
    startNewConversation() {
      this.$root.$emit('start-new-conversation');
    },
    async handleLogout() {
      try {
        // 调用后端登出API
        await logoutFromServer();
        // 清除本地存储
        logout();
        // 跳转到首页
        this.$router.push('/introduction');
        this.$message.success('已成功登出');
      } catch (error) {
        console.error('登出失败:', error);
        this.$message.error('登出失败，请重试');
      }
    },
    async handleDropdownCommand(cmd) {
      if (cmd === 'logout') {
        await this.handleLogout();
      } else if (cmd === 'account') {
        this.$router.push('/account').catch(() => {});
      }
    },
    updateAvatar(url) {
      this.avatar = url;
    }
  },
  mounted() {
    const u = JSON.parse(localStorage.getItem('currentUser') || '{}');
    if (u.avatar) this.avatar = u.avatar;
    this.$root.$on('update-avatar', this.updateAvatar);
  },
  beforeDestroy() {
    this.$root.$off('update-avatar', this.updateAvatar);
  }
};
</script>

<style scoped>
/* ————— 全局基础 ————— */
#main-layout {
  font-family: "Helvetica Neue", Arial, sans-serif;
  background: #f9fafb;
  color: #222;
}

/* ————— 侧边栏 ————— */
.sidebar {
  background: rgba(255,255,255,0.8);
  backdrop-filter: saturate(180%) blur(8px);
  border-right: 1px solid rgba(0,0,0,0.06);
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: width 0.3s;
  overflow: hidden;
  /* 轻微内阴影，突出浮起感 */
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.03);
}

.logo {
  height: 64px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}
.logo:hover {
  background: rgba(0,0,0,0.03);
}
.logo i {
  font-size: 28px;
  color: #030b31;
}
.logo-text {
  margin-left: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #333;
}

.menu {
  flex: 1;
  border: none;
}
.el-menu-item {
  padding: 12px !important;
  transition: background 0.2s, color 0.2s;
}
.el-menu-item i {
  font-size: 20px;
  width: 24px;
  text-align: center;
}
.el-menu-item:hover {
  background: rgba(59,91,253,0.08) !important;
  color: #060d2e !important;
}
.el-menu-item.is-active {
  background: rgba(59,91,253,0.12) !important;
  color: #080f35 !important;
}

.menu-icon {
  width: 20px;
  height: 20px;
  margin-right: 4px;
  vertical-align: middle;
}

/* ————— 用户区域 ————— */
.user-section {
  width: 100%;
  padding: 16px;
  border-top: 1px solid rgba(0,0,0,0.06);
  background: rgba(0,0,0,0.02);
}

.user-info {
  margin-bottom: 12px;
  text-align: center;
}

.username {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.user-role {
  font-size: 12px;
  color: #666;
}

.logout-btn {
  width: 100%;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  border: 1px solid rgba(0,0,0,0.1);
  border-radius: 6px;
  background: rgba(255,255,255,0.8);
  transition: all 0.2s;
}

.logout-btn:hover {
  color: #ff4757;
  border-color: #ff4757;
  background: rgba(255,71,87,0.05);
}

.logout-btn i {
  font-size: 16px;
  margin-right: 8px;
}

.logout-icon {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  vertical-align: middle;
}

.logout-btn span {
  font-size: 14px;
}

/* ————— 主内容区 ————— */
.main-content {
  padding: 0;
  min-height: 100vh;
  overflow: auto;
}
</style>
